Duplicate -y 'Updates:Docs:Open Firmware and Forth:OpenBIOS:JoesOpenBIOSStuff:detok:Output:detok' "{MPWCommands}"

Directory "``Echo {active} | StreamEdit -e "/(:)1/ Change 1"``"
Set romFile 'G5 compiled fcode'
Set dstFile "All.txt"
Flush
#detok -t -v -a -n -o -i -m 4 -s $FF844B00 "{romFile}"  "errors" > "{dstFile}"



open -f `cd`



DumpMacRom -d -s $FF844B00 "{romFile}"


# This script searches for bl instructions that are followed by hex that is not a valid PPC instruction
Open "{dstFile}"
Find  "{dstFile}"
Loop
	Find (/ bl Ŷn[:n]+:[:n]+: ([0-9A-F]4 )+                        /:/ bl[ ]+/):/[ ]+/ "{dstFile}" || Break
	Echo "`Catenate "{dstFile}".`"
End | Sort -unique

$+0
$+21972036
$+4784928
(var)
create-rx-word+20
dabrx!+24
dabrx!+56
exit
install-reg-name+92
throw+20



streamedit "{dstFile}" -d -e "/[ ]*[0-9]+: [0-9A-F]+: ([0-9A-F]2)1[ ]+["]/ Insert 1" | Sort -unique


#========================================================================================================================
# Patches with comments

# 0) this stuff is for creating property names that increment each time that patch creates a property (e.g. romdump_0, romdump_1, romdump_2, ...)
0 value romnum
20 buffer: namebuf
" romdump_"					\	str len
dup constant romdumpLen		\	str len
namebuf						\	str len namebuf
2dup						\	str len namebuf len namebuf
+ constant numpartAddr		\	str len namebuf
swap						\	str namebuf len
move						\

# 1) create a property that lists size and virtual address of rom image - not useful because rom image is unmapped soon after
: domappatch ( phys virt size params -- )
	2over			\	phys virt size params phys virt
	2over			\	phys virt size params phys virt size params
	do-map			\	phys virt size params
	drop			\	phys virt size
	encode-int		\	phys virt sizePropAddr sizePropLen
	2swap			\	sizePropAddr sizePropLen phys virt
	nip				\	sizePropAddr sizePropLen virt
	encode-int		\	sizePropAddr sizePropLen virtPropVAddr virtPropVLen
	encode+			\	propAddr propLen
	romnum			\	propAddr propLen romnum
	(u.)			\	propAddr propLen romnumStr romnumStrLen
	tuck			\	propAddr propLen romnumStrLen romnumStr romnumStrLen
	numpartAddr		\	propAddr propLen romnumStrLen romnumStr romnumStrLen numpartAddr
	swap			\	propAddr propLen romnumStrLen romnumStr numpartAddr romnumStrLen
	move			\	propAddr propLen romnumStrLen
	namebuf			\	propAddr propLen romnumStrLen namebuf
	swap			\	propAddr propLen namebuf romnumStrLen
	romdumpLen		\	propAddr propLen namebuf romnumStrLen romdumpLen
	+				\	propAddr propLen namebuf namelen
	property		\
	;

# 2) this is the same as (1) except that it doesnt use incrementing property name which is only necessary if more than one rom could be mapped per device
: domappatch ( phys virt size params -- )
	2over			\	phys virt size params phys virt
	2over			\	phys virt size params phys virt size params
	do-map			\	phys virt size params
	drop			\	phys virt size
	encode-int		\	phys virt sizePropAddr sizePropLen
	2swap			\	sizePropAddr sizePropLen phys virt
	nip				\	sizePropAddr sizePropLen virt
	encode-int		\	sizePropAddr sizePropLen virtPropVAddr virtPropVLen
	encode+			\	propAddr propLen
	" romdump"		\	propAddr propLen namestr namelen
	property		\
	;

# 3) this creates a property of the rom image contents which is what we really want
: domappatch ( phys virt size params -- )
	2over			\	phys virt size params phys virt
	2over			\	phys virt size params phys virt size params
	do-map			\	phys virt size params
	drop			\	phys virt size
	dup				\	phys virt size size
	20000			\	phys virt size size 20000
	>				\	phys virt size size>20000
	if				\	phys virt size
		drop		\	phys virt
		1000		\	phys virt 1000				( dictionary space is limited so assume that roms greater than 128k are not really roms and just grab the first 4k )
	then
	encode-bytes	\	phys propAddr propLen
	" romdump"		\	phys propAddr propLen namestr namelen
	property		\	phys
	drop
	;


# Y) These routines can be used to find the offset of a token

: make-token-instruction ( to from )
	- 3FFFFFF and 48000001 or ;


: find-token-instruction { tokeaddr searchstart ; searchwhere exitaddr }
	cr
	['] exit -> exitaddr
	searchstart -> searchwhere
	begin
		tokeaddr searchwhere make-token-instruction
		searchwhere l@ =
		if
			searchwhere searchstart -
			0
		else
			exitaddr searchwhere make-token-instruction
			searchwhere l@ =
			if
				-1
				0
			else
				1
			then
		then
		while
			searchwhere 4 + -> searchwhere
	repeat
	;


: find-all-token-instructions { tokeaddr searchstart ; searchwhere curoffset }
	searchstart -> searchwhere
	begin
		tokeaddr searchwhere find-token-instruction -> curoffset
		curoffset -1 <>
		while
			curoffset searchwhere + -> searchwhere
			searchwhere searchstart - .
			searchwhere 4 + -> searchwhere
	repeat
	;

\ examples:
' do-map ' probe-fcode find-token-instruction
' b<lit> ' probe-fcode find-all-token-instructions
' config-l@ ' probe-fcode find-all-token-instructions







# Z) this does the patching by replaceing the call to do-map in probe-fcode
1bc4 get-token drop a8 + ' domappatch blpatch			\ ' probe-fcode a8 + ' domappatch blpatch


#========================================================================================================================

: domappatch 2over 2over do-map drop dup 20000 > if drop 1000 then encode-bytes " romdump" property drop ;
1bc4 get-token drop a8 + ' domappatch blpatch


#========================================================================================================================
# Note: telnet cant be used to edit nvram scripts (at least not the telnet used with Terminal.app)
#========================================================================================================================

probe-fcode
  {1} has-fcode? off fcode-image off h#fffff800 {0@} >pci0.rombase tuck config-l! config-l@ ?dup if
    h#fffff800 and negate dup {2!} ~rom-top swap - dup {1!} 1 or {0@} >pci0.rombase config-l! 2 {0@} set-command-bits {2@} 0> if
      {1@} dup {2@} _i_g do-map
      else
      0 {1!}
      then
    (l 1 sys if
      cr ff8851e8 ."me "  device is requesting "type {2@} u. " , was assigned "
      type {1@} u.
      then
    l) {1@} ?dup if
      {3!}
      begin
        (l 1 sys if
          cr ff8851e8 ."me "  testing "type {3@} dup u. 2c@-le u.
          then
        l) (l 2 dac if
          {3@} 64 dumpl
          then
        l) {3@} dup if
          2c@-le aa55 =
          then
        while
          {3@} dup 18 + 2c@-le + {4!} {4@} 10 + 2c@-le 9 lshift {5!} {4@} 4c@-le 52494350 = if
            {4@} 14 + c@ 1 = dup if
              {3@} fcode-image !
              then
            {4@} cell+ 4c@-le {0@} config-l@ = and {4@} d + 3c@-le {0@} >pci.revisionid.classcode config-l@ 8 rshift = and if
              (l 1 sys if
                cr ff8851e8 ."me "  valid, evaluating "type
                then
              l) {1@} {3@} {0@} eval-fcode 0 {3!}
              else
              {4@} 15 + c@ 80 and if
                0 {3!}
                else
                {3@} {5@} + {3!}
                then
              then
            else
            0 {3!}
            then
        repeat
      then
    {1@} ?dup if
      {2@} do-unmap
      then
    0 {0@} >pci0.rombase config-l! 3 {0@} clr-command-bits
    then
  fcode-image @ dup if
    (l 1 sys if
      cr ff8851e8 ."me "  image found at "type dup u.
      then
    l)
    else
    (l 1 sys if
      cr ff8851e8 ."me "  no fcode image found"type
      then
    l)
    then
  drop ;

#========================================================================================================================


#detok -t -v -a -n -o -i -m 1 '@FFF30000 len-30000 OF' > All2.txt  errors2

comparefiles 'Work:DumpNameRegistry:ROM 8600:untitled folder:All.txt' 'Work:DumpNameRegistry:ROM 8600:All.txt'


detok -t -v -a -n -o -i -m 1 '@FFF30000 len-30000 OF' > All2.txt  errors2

detok -t -v -a -n -o -i -m 1 '8600 compiled fcode' > All.txt  errors


detok -t -v -a -n -o -i -m 1 '8600 compiled fcode' > All4.txt  errors4
detok -t -v -a -n -o -i -m 1 '8600 compiled fcode' > All5.txt  errors5

delete All3.txt
comparefiles All3.txt All4.txt
comparefiles errors3 errors4

detok -t -v -a -i -m 1 '8600 compiled fcode2' > All2.txt  errors2

cd 'Work:DumpNameRegistry:ROM 8600:compiledfcode:'

detok -t -v -a -n -o -i -m 1 '8600 compiled fcode' > All5.txt  errors5
streamedit 'Work:DumpNameRegistry:ROM 8600:compiledfcode:errors5' -e "!/width:/ delete"
streamedit 'Work:DumpNameRegistry:ROM 8600:compiledfcode:errors5' -e "!/definition for/ delete"
streamedit 'Work:DumpNameRegistry:ROM 8600:compiledfcode:errors5' -e "!/good name/ delete"

cd "Work:DumpNameRegistry:ROM 8600:compiledfcode:"
DumpMacRom -d '8600 compiled fcode'
beep

cd "Work:DumpNameRegistry:ROM 8600:"
DumpMacRom '@FFF30000 len-30000 OF'
beep



# Mark Colon Definition Functions
# WARNING: there are too many symbols for this to be any good so dont do it!

Set theFile 'Work:DumpNameRegistry:ROM 8600:Notes:Part2.of'
Find  "{theFile}"
Loop
	Find /: [ nt]+/ "{theFile}" || Break
	If "``Catenate "{theFile}".``" =~ /: ()1/
		If " {{1}}" =~ / '-'/
			Set 1 " {{1}}"
		End
		If " {{1}}" !~ / colon_definition_function/
		If " {{1}}" !~ / colon_string/
		If " {{1}}" !~ / colon_const/
			Mark -n  "{{1}}"
		End
		End
		End
	Else
		Echo "# ba colon"
	End
End


set echo 0
 set echo 0


# Mark rom images
Set theFile 'Work:DumpNameRegistry:ROM 8600:Notes:Part2.of'
Find  "{theFile}"
Set num 1
Loop
	Mark -y /fcode-version[ tn]*/ " fcode {num}" "{theFile}" || Break
	Evaluate num += 1
End

# Mark new-device
Set theFile 'Work:DumpNameRegistry:ROM 8600:Notes:Part2.of'
Find  "{theFile}"
Set num 1
Loop
	Mark -y /[t]*'new-device'[ tn]/ "  new-device {num}" "{theFile}" || Break
	Evaluate num += 1
End

# Mark device names
Set theFile 'Work:DumpNameRegistry:ROM 8600:Notes:Part2.of'
Find  "{theFile}"
Set num 1
Loop
	Find ((/[t]*'" '[¶"n]+'"'Ŷn[t]*'device-name'[ tn]/):/'" '/):/[¶"]+/ "{theFile}" || Break
	Set theName "``Catenate "{theFile}".``"
	Mark -y  "device-name {{theName}}" "{theFile}"
End


MacOFRomFixLocals 'Work:DumpNameRegistry:ROM 8600:Notes:Part2.of'
